package com.some.jobdescapp.presenter;

import com.randy.recyclerviews.adapter.ExpandableList;
import com.randy.recyclerviews.adapter.ExpandableListAdapter;
import com.randy.recyclerviews.adapter.UniversalAdapter;
import com.some.jobdescapp.bean.ChildEntity;
import com.some.jobdescapp.bean.GroupEntity;
import com.some.jobdescapp.bean.JobDescEntity;
import com.some.jobdescapp.model.BannerModel;
import com.some.jobdescapp.model.ContentModel;
import com.some.jobdescapp.model.TitleModel;
import com.some.jobdescapp.view.IHomeView;

import java.util.ArrayList;
import java.util.List;

/**
 * @author RANDY.ZHANG
 * @version 1.0
 * @company:
 * @date 2019/1/8 21:30
 */
public class HomePresenter {
    private IHomeView view;
    private ExpandableListAdapter adapter;
    public HomePresenter(IHomeView view) {
        this.view = view;

        adapter = new ExpandableListAdapter();
        view.bindAdapter(adapter);
    }

    public void initDemoData() {
        adapter.addHeader(new BannerModel());
        List<ExpandableList> lists = new ArrayList<>();
        List<GroupEntity> groupEntities = mockData();

        for (GroupEntity groupEntity : groupEntities) {
            TitleModel titleModel = new TitleModel();
            ExpandableList expandableList = new ExpandableList(titleModel, null, null);
            titleModel.setGroupEntity(groupEntity);
            titleModel.setExpandableList(expandableList);
            expandableList.setCollapsed(groupEntity.isCollapsed());
            List<UniversalAdapter.AbstractModel<?>> childModels = expandableList.getChildModels();
            for (ChildEntity childEntity : groupEntity.getChildNodes()) {
                ContentModel contentModel = new ContentModel(childEntity);
                childModels.add(contentModel);
            }
            lists.add(expandableList);
        }

        adapter.updateDataList(lists);
    }


    private List<GroupEntity> mockData() {
        List<GroupEntity> list = new ArrayList<>();
        for (int i = 0; i < 5; i++) {
            GroupEntity groupEntity = new GroupEntity();
            List<ChildEntity> childNodes = groupEntity.getChildNodes();
            groupEntity.setId(i);
            childNodes.clear();
            groupEntity.setCollapsed(true);
            groupEntity.setTitle("Title : " + i);
            for (int i1 = 0; i1 < 6; i1++) {
                ChildEntity childEntity = new ChildEntity();
                childEntity.setId(i * 100 + i1);
                childEntity.setContent("content : " + childEntity.getId());
                fillJobDesc(childEntity);
                childNodes.add(childEntity);
            }
            list.add(groupEntity);
        }
        return list;
    }

    private void fillJobDesc(ChildEntity childEntity) {
        List<JobDescEntity> jobDesc = childEntity.getJobDesc();
        for (int i = 0; i < 2; i++) {
            JobDescEntity jobDescEntity = new JobDescEntity();
            jobDescEntity.setTitle("责任" + (i + 1));
            jobDescEntity.setDesc("责任" +  + (i + 1) + "描述");
            jobDesc.add(jobDescEntity);
        }
    }
}
